package com.syqc.monitor.ui.alarm

import android.view.View
import android.widget.ImageView
import androidx.recyclerview.widget.LinearLayoutManager
import autodispose2.autoDispose
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.syqc.comlib.base.BaseRecycleActivity
import com.syqc.comlib.utils.ToolbarHelper
import com.syqc.comlib.utils.showToast
import com.syqc.comlib.utils.startActivityTo
import com.syqc.comlib.utils.toast
import com.syqc.custom.LeftRightTextView
import com.syqc.entity.HistoryAlarm
import com.syqc.monitor.R
import com.syqc.monitor.databinding.ActivityToolbarListBinding
import com.syqc.monitor.ui.realalarm.MediaInfoActivity
import com.syqc.net.ServiceHelper
import com.syqc.utils.DataUtil
import com.syqc.utils.Keys
import com.syqc.utils.TimeUtil
import com.syqc.utils.ToolsUtil
import com.syqc.utils.Units

/**
 *@Author Jasper
 *@Time   2020/12/9
 *@Desc   报警详情页
 */
class AlarmDetailActivity :
    BaseRecycleActivity<ActivityToolbarListBinding>(ActivityToolbarListBinding::inflate) {

    private var isAdas = false

    override fun initView() {
        super.initView()
        val data: ArrayList<HistoryAlarm.TotalAlarms.InfoAlarms>? =
            DataUtil.alarmInfo
        isAdas = intent.getBooleanExtra(Keys.INTENT_OTHER, false)
        val carId = intent.getStringExtra(Keys.INTENT_CAR_ID) ?: ""
        val plate = intent.getStringExtra(Keys.INTENT_PLATE) ?: ""
        ToolbarHelper.setToolbar(this, intent.getStringExtra(Keys.INTENT_TITLE) ?: "")
        mRecyclerVB.recyclerView.layoutManager = LinearLayoutManager(this)
        val mAdapter = MAdapter()
        mRecyclerVB.recyclerView.adapter = mAdapter
        mAdapter.setList(data)
        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            if (ToolsUtil.isFastClick()) return@setOnItemChildClickListener
            isShowLoading(true)
            val entity = mAdapter.data[position]
            getAlarmInfo(carId, entity.stime, entity.alarmType, plate)
        }
    }

    override fun setListener() {

    }

    private fun getAlarmInfo(catId: String, startTime: String, alarmType: String, plate: String) {
        ServiceHelper.getRepository().getAlarmMedia(
            catId,
            TimeUtil.instance.formatHm(startTime),
            alarmType
        )
            .autoDispose(scopeProvider).subscribe({
                if (it.isNullOrEmpty()) {
                    toast(R.string.desc_no_about_data)
                    isShowLoading(false)
                    return@subscribe
                }
                startActivityTo<MediaInfoActivity>(this) {
                    putExtra(Keys.INTENT_TITLE, plate)
                    putExtra(Keys.INTENT_CAR_ID, catId)
                    putParcelableArrayListExtra(Keys.INTENT_DATA, it)
                }
                isShowLoading(false)
            }, {
                showToast(it.message ?: "")
                isShowLoading(false)
            })
    }


    inner class MAdapter :
        BaseQuickAdapter<HistoryAlarm.TotalAlarms.InfoAlarms, BaseViewHolder>(R.layout.item_alarm_detail) {
        init {
            addChildClickViewIds(R.id.iv_alarm_detail_more)
        }

        override fun convert(
            holder: BaseViewHolder,
            item: HistoryAlarm.TotalAlarms.InfoAlarms
        ) {
            item.run {
                val position = holder.layoutPosition + 1
                val num = if (position < 10) "0$position" else "$position"
                holder.setText(R.id.tv_alarm_detail_num, num)
                holder.setText(R.id.tv_alarm_detail_address, addr)
                holder.getView<ImageView>(R.id.iv_alarm_detail_more).visibility =
                    if (fjNum == "0") View.GONE else View.VISIBLE
                holder.getView<LeftRightTextView>(R.id.lr_tv_alarm_detail_time)
                    .setRightText("$stime ~ $etime")
                holder.getView<LeftRightTextView>(R.id.lr_tv_alarm_detail_long)
                    .setRightText("$speed ${Units.speed}")
            }
        }
    }
}